import math
import json


class Reward:
    def __init__(self):
        pass

    @staticmethod
    def zero_reward(state, action):
        return 0

    @staticmethod
    def area_reward(state, action, alpha=0):
        area = state.get_trace().covered_area(state.actions, has_last_point=False, alpha=alpha)
        return (state.get_trace().baseline_area - area) / state.get_trace().baseline_area

    @staticmethod
    def delay_reward(state, action, miu1=1, deadline=309.97, miu2=1, act_error_path="", c1=1, c2=1):  # TODO: 需修改
        new_time_pre = state.get_prediction_sub_sequence()[-1]
        r_risk = 2 / math.pi * math.atan(miu1 * max(0, new_time_pre-deadline))

        act = state.get_trace().get_activity_names()[state.next_index-1+action]
        fr = open(act_error_path, "r")
        act_errors = json.load(fr)
        act_error = 0
        if act in act_errors.keys():
            act_error = act_errors[act]
        dt = state.get_trace_times()[state.next_index-1+action] - state.get_trace_times()[state.actions[-1]]
        r_pro = 2 / math.pi * math.atan(miu2 * dt - act_error)

        return c1 * r_risk + c2 * r_pro
